﻿Imports System.Data.OleDb

Public Class Facturation
    Private _persoonId As String
    Private _bedrijfsNaam As String
    Private _adresLijn1 As String
    Private _adresLijn2 As String
    Private _postCode As String
    Private _plaats As String
    Private _land As String

    Public Sub New(ByVal persoonId As String, ByVal bedrijfsNaam As String, ByVal adreslijn1 As String, ByVal adreslijn2 As String, ByVal postCode As String, ByVal plaats As String, ByVal land As String)
        Me._persoonId = persoonId
        Me._bedrijfsNaam = bedrijfsNaam
        Me._adresLijn1 = adreslijn1
        Me._adresLijn2 = adreslijn2
        Me._postCode = postCode
        Me._plaats = plaats
        Me._land = land
    End Sub

    Public Property PersoonId As String
        Get
            Return _persoonId
        End Get
        Set(ByVal Value As String)
            _persoonId = Value
        End Set
    End Property

    Public Property BedrijfsNaam As String
        Get
            Return _bedrijfsNaam
        End Get
        Set(ByVal Value As String)
            _bedrijfsNaam = Value
        End Set
    End Property

    Public Property AdresLijn1 As String
        Get
            Return _adresLijn1
        End Get
        Set(ByVal Value As String)
            _adresLijn1 = Value
        End Set
    End Property

    Public Property AdresLijn2 As String
        Get
            Return _adresLijn2
        End Get
        Set(ByVal Value As String)
            _adresLijn2 = Value
        End Set
    End Property

    Public Property PostCode As String
        Get
            Return _postCode
        End Get
        Set(ByVal Value As String)
            _postCode = Value
        End Set
    End Property

    Public Property Plaats As String
        Get
            Return _plaats
        End Get
        Set(ByVal Value As String)
            _plaats = Value
        End Set
    End Property

    Public Property Land As String
        Get
            Return _land
        End Get
        Set(ByVal Value As String)
            _land = Value
        End Set
    End Property

    Public Shared Function getFacturationForPerson(ByRef Person As Person, ByRef dbh As DatabaseHelper) As Facturation
        Dim facturation As Facturation
        Dim reader As OleDbDataReader = dbh.GetReader("SELECT * FROM FacturatieGegevens WHERE PerId = ?", {Person.Id})
        If Not reader.Read() Then
            'insert standard data
            dbh.ExecuteSQL("INSERT INTO FacturatieGegevens VALUES(?,?,?,?,?,?,?)", {Person.Id, "", Person.Adreslijn1, Person.Adreslijn2, Person.Postcode, Person.Plaats, Person.Land}, True)
            'read again
            reader = dbh.GetReader("SELECT * FROM FacturatieGegevens WHERE PerId = ?", {Person.Id})
            reader.Read()
        End If
        facturation = New Facturation(CStr(reader("PerId")), CStr(reader("FacBedrijfsnaam")), CStr(reader("FacAdreslijn1")), CStr(reader("FacAdreslijn2")), CStr(reader("FacPostcode")), CStr(reader("FacPlaats")), CStr(reader("FacLand")))
        reader.Close()
        Return facturation
    End Function

    Public Shared Sub updateFacturation(ByRef Facturation As Facturation, ByRef dbh As DatabaseHelper)
        dbh.ExecuteSQL("UPDATE FacturatieGegevens SET FacBedrijfsnaam = ? , FacAdreslijn1 = ? , FacAdreslijn2 = ? , FacPostcode = ? , FacPlaats = ? , FacLand=? WHERE PerId= ?", {Facturation.BedrijfsNaam, Facturation.AdresLijn1, Facturation.AdresLijn2, Facturation.PostCode, Facturation.Plaats, Facturation.Land, Facturation.PersoonId}, True)

    End Sub

    Public Shared Sub deleteFacturationForPerson(ByRef Person As Person, ByRef dbh As DatabaseHelper)
        dbh.ExecuteSQL("DELETE FROM FacturatieGegevens WHERE PerId=?", {Person.Id}, True)
    End Sub
End Class
